<template>
  <el-dialog
      title="添加/修改教室预约信息"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="教室编号" prop="activityRoomApplication.classroomNumber" size="small">
        <el-input size="small" v-model="activityRoomApplication.classroomNumber"></el-input>
      </el-form-item>
      <el-form-item label="活动日期" prop="activityRoomApplication.activityDate" size="small">
        <el-input size="small" v-model="activityRoomApplication.activityDate"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="activityRoomApplication.activeTime" size="small">
        <el-input size="small" v-model="activityRoomApplication.activeTime"></el-input>
      </el-form-item>
      <el-form-item label="活动时长" prop="activityRoomApplication.activityTime" size="small">
        <el-input size="small" v-model="activityRoomApplication.activityTime"></el-input>
      </el-form-item>
      <el-form-item label="申请人" prop="activityRoomApplication.applicant" size="small">
        <el-input size="small" v-model="activityRoomApplication.applicant"></el-input>
      </el-form-item>
      <el-form-item label="申请时间" prop="activityRoomApplication.applicationTime" size="small">
        <el-input size="small" v-model="activityRoomApplication.applicationTime"></el-input>
      </el-form-item>
      <el-form-item label="活动描述" prop="activityRoomApplication.activityDescription" size="small">
        <el-input size="small" v-model="activityRoomApplication.activityDescription"></el-input>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改预约信息</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getActivityRoomApplicationById, updateActivityRoomApplication, createActivityRoomApplication} from "@/api/activityRoomApplication";
import Formatter from "@/utils/formatter";
export default {
  name: 'ActivityRoomApplicationEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      activityRoomApplication: {}
    }
  },
  methods: {

    // 添加教室信息
    onSubmit() {
      createActivityRoomApplication({
        'activeTime': this.activityRoomApplication.activeTime,
        'activityDate': this.activityRoomApplication.activityDate,
        'activityDescription': this.activityRoomApplication.activityDescription,
        'activityNumber': this.activityRoomApplication.activityNumber,
        'activityTime':this.activityRoomApplication.activityTime,
        'applicant': this.activityRoomApplication.applicant,
        'classroomId': this.activityRoomApplication.classroomId,
        'classroomNumber':this.activityRoomApplication.classroomNumber,
        'applicationTime':this.activityRoomApplication.applicationTime,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新教室信息
    handleUpdate() {
      console.log(this.activityRoomApplication)
      updateActivityRoomApplication({
        'id': this.activityRoomApplication.id,
        'activeTime': this.activityRoomApplication.activeTime,
        'activityDate': this.activityRoomApplication.activityDate,
        'activityDescription': this.activityRoomApplication.activityDescription,
        'activityNumber': this.activityRoomApplication.activityNumber,
        'activityTime':this.activityRoomApplication.activityTime,
        'applicant': this.activityRoomApplication.applicant,
        'classroomId': this.activityRoomApplication.classroomId,
        'classroomNumber':this.activityRoomApplication.classroomNumber,
        'applicationTime':this.activityRoomApplication.applicationTime,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getActivityRoomApplicationById(this.id).then((response) => {
          this.activityRoomApplication = response.data.data;
          // this.activityRoomApplication.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.activityRoomApplication = {}
      this.$emit('update:visible', false)
    },

    // 格式化时间
    formatDate(date) {
      return Formatter.formatDate(date, 'YYYY-MM-DD')
    }
  }
}
</script>

<style scoped>

</style>
